import { defineStore } from 'pinia';

export const userStore = defineStore('user', {
    state: () => ({
        user: {
            avatar: uni.getStorageSync('avatar') || '',
            nickname: uni.getStorageSync('nickname') || '',
            token: uni.getStorageSync('token') || null
        }
    }),
    actions: {
        setUser(user) {
            this.user.avatar = user.avatar || '';
            this.user.nickname = user.nickname || '';
            this.user.token = user.token || null;
            uni.setStorageSync('avatar', this.user.avatar);
            uni.setStorageSync('nickname', this.user.nickname);
            uni.setStorageSync('token', this.user.token);
        },
        updateUser(user) {
            this.user.avatar = user.avatar || '';
            this.user.nickname = user.nickname || '';
            uni.setStorageSync('avatar', this.user.avatar);
            uni.setStorageSync('nickname', this.user.nickname);
        },
        clearUser() {
            this.user = {
                avatar: '',
                nickname: '',
                token: null
            };
            uni.removeStorageSync('avatar');
            uni.removeStorageSync('nickname');
            uni.removeStorageSync('token');
        }
    }
});